<?php
/**
 * Created by PhpStorm.
 * User: lenovo
 * Date: 2021/5/21
 * Time: 15:03
 */

namespace app\controller;

use app\BaseController;
use app\service\ArticleService;
use app\service\SearchEnginePushService;
use think\facade\View;
use think\facade\Request;

class Article extends Base
{
    public function list(){
        // 模板变量赋值
        $ArticelService = new ArticleService();
        $page =  Request::param('page',0);
        $size =  20;
        $list = $ArticelService->getList($page,$size);
        if(count($list)==0){
            View::assign('next_page',$page);
        }else{
            View::assign('next_page',$page+1);
        }
        if($page==0){
            View::assign('prepage',$page);
        }else{
            View::assign('prepage',$page-1);
        }
        View::assign('list',$list);
        // 模板输出
        return View::fetch('list');
    }

    /**页面详情
     * @return string
     * @throws \Exception
     */
    public function detail()
    {
        $id = Request::param('id');
        // 模板变量赋值
        $articelService = new ArticleService();
        $article = $articelService->detail($id);
        $myList = array();
        View::assign('article',$article);
        View::assign('myList',$myList);
        // 模板输出
        return View::fetch('detail');
    }

    /**
     * appkey正确才能添加
     */
    public function add(){
        $serverAppKey = env('app_key');
        $appKey = Request::param('app_key');
        $param = Request::param();
        if( $appKey!=$serverAppKey){
            return json(array('code'=>0));
        }

        $ArticelService = new ArticleService();
        $ret =      $ArticelService->add($param);
        $searchEnginePushService = new SearchEnginePushService();
        $searchEnginePushService->baidu($ret);
        return json(array('code'=>200));

    }
}